/* dialog.h */

#ifndef DIALOG_H_FILE
#define DIALOG_H_FILE

#include "SDL.h"

#define DLG_MAX_WIDGETS 32

/* dialog messages */
enum {
  DLGM_INIT,
  DLGM_DESTROY,
  DLGM_REDRAW,
  DLGM_IDLE,
  DLGM_GAIN_FOCUS,
  DLGM_LOSE_FOCUS,
  DLGM_SDL_EVENT,
};

/* dialog notifications */
enum {
  DLGN_INIT,
  DLGN_CLICKED,
  DLGN_ENTER,
  DLGN_CHANGED,
};

typedef struct DLG_WIDGET DLG_WIDGET;
typedef struct DIALOG DIALOG;

typedef void (*dlg_widget_notify)(DLG_WIDGET *widget, int note);
typedef int (*dlg_widget_callback)(DLG_WIDGET *widget, int msg, unsigned int p1, unsigned int p2, void *p);

struct DLG_WIDGET {
  dlg_widget_callback func;
  int x, y;
  int w, h;
  void *p;
  unsigned int d1, d2, d3;
  dlg_widget_notify note_func;

  DIALOG *dlg;
  int get_all_key_events;  /* 1 to get key events even when not focused */
};

struct DIALOG {
  DLG_WIDGET widget[DLG_MAX_WIDGETS];

  /* used by dlg_run(): */
  int num_widgets;
  DLG_WIDGET *focus;
  int stop;
  int update_screen;
  SDL_Surface *screen;
};

#define DLG_WIDGET_SCREEN(bg_color, func)             { dlg_w_screen,   0, 0, 0, 0, NULL, 0, 0, (bg_color), (func) }
#define DLG_WIDGET_LABEL(x, y, text)                  { dlg_w_label,    (x), (y), 0, 0, (text), 0, 0, 0 }
#define DLG_WIDGET_TEXT(x, y, w, text)                { dlg_w_text,     (x), (y), (w), 0, (text), 0, 0, 0 }
#define DLG_WIDGET_BUTTON(x, y, w, text, func)        { dlg_w_button,   (x), (y), (w), 0, (text), 0, 0, 0, (func) }
#define DLG_WIDGET_EDIT(x, y, w, buf, buf_size, func) { dlg_w_edit,     (x), (y), (w), 0, (buf), (buf_size), 0, 0, (func) }
#define DLG_WIDGET_DROPDOWN(x, y, w, items, func)     { dlg_w_dropdown, (x), (y), (w), 0, (items), 0, 0, 0, (func) }

int dlg_run(DIALOG *dlg, SDL_Surface *screen, int initial_focus);
void dlg_end(DIALOG *dlg);
int dlg_send_msg(DLG_WIDGET *widget, int msg, unsigned int p1, unsigned int p2, void *p);
void dlg_broadcast_msg(DIALOG *dlg, int msg, unsigned int p1, unsigned int p2, void *p);
DLG_WIDGET *dlg_find_widget_at(DIALOG *dlg, int x, int y);
int dlg_msg_is_button_press(DLG_WIDGET *widget, int msg, unsigned int p1, unsigned int p2, void *p);

int dlg_w_screen(DLG_WIDGET *widget, int msg, unsigned int p1, unsigned int p2, void *p);
int dlg_w_label(DLG_WIDGET *widget, int msg, unsigned int p1, unsigned int p2, void *p);
int dlg_w_text(DLG_WIDGET *widget, int msg, unsigned int p1, unsigned int p2, void *p);
int dlg_w_button(DLG_WIDGET *widget, int msg, unsigned int p1, unsigned int p2, void *p);
int dlg_w_edit(DLG_WIDGET *widget, int msg, unsigned int p1, unsigned int p2, void *p);
int dlg_w_dropdown(DLG_WIDGET *widget, int msg, unsigned int p1, unsigned int p2, void *p);

#endif /* DIALOG_H_FILE */
